package com.pactera.madp.cp.controller.oms.sensitive;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.common.security.annotation.Inner;
import com.pactera.madp.cp.api.dto.sensitive.SensitiveWordAddDTO;
import com.pactera.madp.cp.api.dto.sensitive.SensitiveWordUpdateDTO;
import com.pactera.madp.cp.api.em.permission.ReqPerEnum;
import com.pactera.madp.cp.api.dto.sensitive.ReqSensitiveWordDto;
import com.pactera.madp.cp.api.entity.CorpEntity;
import com.pactera.madp.cp.api.vo.SensitiveWorldVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.ICorpService;
import com.pactera.madp.cp.service.ISensitiveWordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:敏感词库管理
 * @author: Huayu
 * @time: 2021/1/27 10:29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sensitiveword")
@Api(value = "sensitiveword", tags = "敏感词库管理")
public class SensitiveWordController {

    private ISensitiveWordService sensitiveWordServiceImpl;
    private ICorpService corpService;

    /**
     *
     * @description:删除敏感词
     * @author: Huayu
     * @time: 2021/1/27 11:26
     */
    @SysLog("删除敏感词")
    @DeleteMapping("/{sensitiveWordId}")
    @ApiOperation(value = "删除敏感词")
    @ApiImplicitParams({
            @ApiImplicitParam(name="sensitiveWordId",value="敏感词id",dataType ="Integer")
    })
    public R<Boolean> deleteSensitiveWord(@PathVariable Integer sensitiveWordId){
        return sensitiveWordServiceImpl.deleteSensitiveWord(sensitiveWordId) ? R.ok(Boolean.TRUE,"删除成功"):R.failed(Boolean.FALSE,"删除失败");
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "敏感词详情")
    public R<SensitiveWorldVO> getSensitiveWord(@PathVariable Integer id){
        return R.ok(sensitiveWordServiceImpl.getSensitiveWord(id));
    }

    @SysLog("修改敏感词")
    @PutMapping("/update")
    @ApiOperation(value = "修改敏感词")
    public R<Boolean> updateSensitiveWord(@RequestBody SensitiveWordUpdateDTO sensitiveWordUpdateDTO){
        return sensitiveWordServiceImpl.updateSensitiveWord(sensitiveWordUpdateDTO) ? R.ok(Boolean.TRUE,"更新成功") : R.ok(Boolean.FALSE,"更新失败");
    }

    /**
     *
     * @description:敏感词列表
     * @author: Huayu
     * @time: 2021/1/28 9:07
     */
    @GetMapping("/page")
    @ApiOperation(value = "查询敏感词列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="permission",value="权限:ALL-全部,DEPARTMENT-部门,EMPLOYEE-员工")
    })
    public R<Page<List<SensitiveWorldVO>>> sensitiveWordIndex(ReqSensitiveWordDto sensitiveWordIndex, ReqPerEnum permission, Page page){
        //判断用户绑定企业信息
        if(CorpContextHolder.getCorpId() == null){
            throw new CheckedException("未选择登录企业，不可操作");
        }
        return R.ok(sensitiveWordServiceImpl.getSensitiveWordList(sensitiveWordIndex, permission,page));
    }

    /**
     *
     * @description:添加敏感词
     * @author: Huayu
     * @time: 2021/1/28 18:30
     */
    @SysLog("添加敏感词")
    @PostMapping
    @ApiOperation(value = "添加敏感词")
    public R<Boolean> sensitiveWordStore(@RequestBody SensitiveWordAddDTO sensitiveWordAddDto){
        return sensitiveWordServiceImpl.createSensitiveWord(sensitiveWordAddDto) ? R.ok(Boolean.TRUE,"添加成功"):R.failed(Boolean.FALSE,"添加失败");
    }

    /**
     *
     * @description:敏感词移动
     * @author: Huayu
     * @time: 2021/1/28 18:30
     */
    @SysLog("敏感词移动分组")
    @PutMapping("/move")
    @ApiOperation(value = "敏感词移动分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name="sensitiveWordId",value="敏感词id",dataType ="Integer"),
            @ApiImplicitParam(name="groupId",value="敏感词库组id",dataType ="Integer")
    })
    public R sensitiveWordMove(Integer sensitiveWordId,String groupId){
        if(groupId == null || groupId.equals("")){
            return R.failed("敏感词ID不能为空");
        }
        if(sensitiveWordId == null){
            return R.failed("敏感词分组ID不能为空");
        }
        String clStr = "group_id";
        Integer i = sensitiveWordServiceImpl.updateSensitiveWordById(sensitiveWordId,clStr,groupId);
        if(i < 1){
            return R.failed("敏感词移动失败");
        }
        return R.ok();
    }

    @SysLog("敏感词变更状态")
    @PutMapping
    @ApiOperation(value = "敏感词变更状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name="sensitiveWordId",value="敏感词id",dataType ="Integer"),
            @ApiImplicitParam(name="status",value="敏感词状态 2--关闭  1--开启",dataType ="String")
    })
    public R sensitiveWordStatusUpdate(Integer sensitiveWordId,String status){
        if(status == null || status.equals("")){
            return R.failed("敏感词状态不能为空");
        }
        if(sensitiveWordId == null){
            return R.failed("敏感词分组ID不能为空");
        }
        //数据入表
        String clStr = "status";
        Integer i = sensitiveWordServiceImpl.updateSensitiveWordById(sensitiveWordId,clStr,status);
        if(i < 1){
            return R.failed("敏感词状态更新失败");
        }
        return R.ok();
    }

    @SysLog("发送消息提醒")
    @Inner
    @PostMapping("/send")
    @ApiOperation(value = "发送消息提醒")
    public void sendNotifications(){
        List<Integer> collect = corpService.list().stream().map(CorpEntity::getCorpId).collect(Collectors.toList());
        collect.forEach(c->{
           sendSensitive(c);
        });
    }

    /**
     * 发送消息提醒异步中转
     * @param corpId
     */
    @Async
    void sendSensitive(Integer corpId){
        sensitiveWordServiceImpl.sendNotifications(corpId);
    }

}
